package com.allen.app.common.config.aspect;

import cn.hutool.core.date.TimeInterval;
import com.allen.app.common.config.other.BusinessException;
import com.allen.app.common.config.dingtalk.DingTalkHelper;
import com.allen.app.common.utils.ThreadManager;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;


/**
 * Description: 异步请求切面控制
 *
 * @author Limiaojun
 * @date 2019/8/1 15:37
 **/
@Aspect
@Component
@Slf4j
public class AsyncRequestAspect {

    private TimeInterval interval = new TimeInterval();

    @Around("@annotation(asyncRequest)")
    public Object arround(ProceedingJoinPoint proceedingJoinPoint, AsyncRequest asyncRequest) {
        ThreadManager.getThreadPool().execute(() -> {
            try {
                log.info("asyncRequest start ... ");
                interval.start();
                proceedingJoinPoint.proceed();
                log.info("asyncRequest end ... Time consuming\t{}ms ", interval.intervalMs());
            } catch (Throwable throwable) {
                DingTalkHelper.send(new BusinessException(throwable));
            }
        });
        return null;
    }

}